package com.team7.happycommunity.propertymanagement.controller;

import com.github.pagehelper.PageInfo;
import com.team7.happycommunity.propertymanagement.common.CommonResult;
import com.team7.happycommunity.propertymanagement.pojo.Announcement;
import com.team7.happycommunity.propertymanagement.pojo.ParkingInfo;
import com.team7.happycommunity.propertymanagement.service.ParkingInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/parking")
@Controller
public class ParkingController {

    @Autowired
    ParkingInfoService parkingInfoService;

    @PostMapping("/add")
    @ResponseBody
    public CommonResult add(String username,@RequestBody ParkingInfo parkingInfo){
        int result = parkingInfoService.save(parkingInfo,username);
        return result==0?CommonResult.failed():CommonResult.success();
    }

    @PostMapping("/del")
    @ResponseBody
    public CommonResult del(Integer id){
        int result = parkingInfoService.delete(id);
        return result==0?CommonResult.failed():CommonResult.success();
    }

    @GetMapping("/mlist")
    @ResponseBody
    public CommonResult getMList(Integer areaid){
        List<ParkingInfo> list = parkingInfoService.getMList(areaid);
        return CommonResult.success(list);
    }

    @GetMapping("/list")
    @ResponseBody
    public CommonResult getList(@RequestParam(value="page",defaultValue = "1",required = false) Integer currentPage,
                                @RequestParam(value="limit",defaultValue = "5",required = false) Integer pageSize,
                                @RequestParam(value = "username",defaultValue = "null",required = false) String username){

        PageInfo info = null;
        try{
            List<ParkingInfo> list = parkingInfoService.getList(currentPage,pageSize,username);
            info = new PageInfo(list);
            return CommonResult.success(info);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

}
